from flask_login import current_user
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import StringField, SubmitField, PasswordField, BooleanField
from wtforms.validators import DataRequired, Email, ValidationError, EqualTo, Length

from .models import User


class ResetRequestForm(FlaskForm):
    """发送重置密码邮箱的表单，只有一个邮箱输入框"""
    email = StringField(
        '邮箱',
        validators=[DataRequired(message='邮箱不能为空'), Email(message='无效邮箱地址')]
    )
    submit = SubmitField('发送')

    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user is None:
            raise ValidationError('该邮箱尚未注册，请查看输入邮箱地址是否有误')


class PasswordResetForm(FlaskForm):
    """密码重置表单，输入框有密码和确认密码"""
    password = PasswordField(
        '密码',
        validators=[DataRequired(message='密码不能为空')]
    )
    password2 = PasswordField(
        '确认密码',
        validators=[DataRequired(message='确认密码不能为空'), EqualTo('password')]
    )
    submit = SubmitField('重置')


class ProfileForm(FlaskForm):
    """个人中心表单，用户可以更改用户名，邮箱地址以及个人头像信息"""
    username = StringField(
        '用户名',
        validators=[
            DataRequired(message='用户名不能为空'),
            Length(min=2, max=32, message='用户名长度必须在2到32个字符之间')
        ]
    )
    email = StringField(
        '邮箱',
        validators=[DataRequired(message='邮箱不能为空'), Email(message='无效邮箱地址')]
    )

    avatar = FileField('头像', validators=[FileAllowed(['jpg', 'jpeg', 'png'], message='只支持jpg、jpeg和.png图片')])
    submit = SubmitField('保存')

    def validate_username(self, username):
        # 只有当用户输入的用户名不同时才进行校验
        if username.data != current_user.username:
            user = User.query.filter_by(username=username.data).first()
            if user:
                raise ValidationError('该用户已经注册，请选择其他用户名')

    def validate_email(self, email):
        # 只有当用户输入的邮箱不同时才进行校验
        if email.data != current_user.email:
            user = User.query.filter_by(email=email.data).first()
            if user:
                raise ValidationError('该邮箱已经注册，请选择其他邮箱')


class RegistrationForm(FlaskForm):
    """注册表单"""
    username = StringField(
        '用户名',
        validators=[
            DataRequired(message='用户名不能为空'),
            Length(min=2, max=32, message='用户名长度必须在2到32个字符之间')
        ]
    )
    email = StringField(
        '邮箱',
        validators=[DataRequired(message='邮箱不能为空'), Email(message='无效邮箱地址')]
    )
    password = PasswordField(
        '密码',
        validators=[DataRequired(message='密码不能为空')]
    )
    password2 = PasswordField(
        '确认密码',
        validators=[DataRequired(message='确认密码不能为空'), EqualTo('password')]
    )

    submit = SubmitField('注册')

    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user:
            raise ValidationError('该用户已经注册，请选择其他用户名')

    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user:
            raise ValidationError('该邮箱已经注册，请选择其他邮箱')


class LoginForm(FlaskForm):
    """登录表单"""
    email = StringField(
        '邮箱',
        validators=[DataRequired(message='邮箱不能为空'), Email(message='无效邮箱地址')]
    )
    password = PasswordField(
        '密码',
        validators=[DataRequired(message='密码不能为空')]
    )
    remember_me = BooleanField('记住我')
    submit = SubmitField('登录')
